// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameData from "../gameData";
import Tool from "../../framework/Tool";
import AudioManage, {soundList} from "../../framework/AudioManage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class pieceMediator extends cc.Component {

    @property({displayName : "碎片的顺序"})
    pieceIndex = 0;

    @property()
    isMaPiece : boolean = false;

    @property()
    isDragonBall : boolean = false;

    @property()
    isClown : boolean  = false;

    @property(cc.Node)
    maskNode : cc.Node = null;

    @property()
    isTouch : boolean = false;

    onLoad () {
        // if(this.isClown){
        //     if(gameData.colorfulPiece[3][this.pieceIndex - 1] == 1){
        //         this.node.active = false;
        //     }
        // }else if(this.isDragonBall){
        //     if(gameData.colorfulPiece[2][this.pieceIndex - 1] == 1){
        //         this.node.active = false;
        //     }
        //     if(this.isTouch){
        //         this.node.on("touchend",this.showView,this);
        //     }
        // }else if(this.isMaPiece){
        //     if(gameData.colorfulPiece[0][this.pieceIndex - 1] == 1){
        //         this.node.active = false;
        //     }
        // }else {
        //     if (gameData.colorfulPiece[1][this.pieceIndex - 1] == 1) {
        //         this.node.active = false;
        //     }
        // }
        this.node.active = false;
    }


    isClick = false;
    showView(event){
        if(this.isClick) return;
        this.isClick = true;
        AudioManage.playSFX(soundList.pickTool);
        let pos = cc.v2(this.node.x,this.node.y);
        cc.tween(this.node).to(0.3,{
            position : cc.v2(cc.Camera.main.node.x,cc.Camera.main.node.y)
        }).call(()=>{
            this.node.active = false;
            Tool.loadPrefab("prefabs/callDragonBall",(res)=>{
                let view = cc.instantiate(res);
                view.getComponent("callDragonBall").setCallBack(this.pieceIndex,true,()=>{
                    this.node.destroy();
                },()=>{
                    this.isClick = false;
                    this.node.x = pos.x;
                    this.node.y = pos.y;
                    this.node.active = true;
                });
                view.parent = cc.find("Canvas");
            })
        }).start();
    }
}
